package com.tushen.controller;

import com.tushen.dao.CommonDao;
import com.tushen.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;


/**
 * 数据表操作通用接口
 * Created by likun on 2020/8/2.
 */
@Controller
public class CommonDataController {

    /**
     * 增删改任意一表
     * @param table 表名
     * @param operation
     * @param json
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "data/{operation}/{table}", produces = "application/json; charset=utf-8") //用来处理前台的logi
    public Map handleData(@PathVariable("table") String table,
                          @PathVariable("operation") String operation,
                          @RequestBody List<HashMap> json) throws IOException {

        HashMap<String,Object> resultMap=new HashMap<String, Object>();
        HashSet<String> opset=new HashSet<String>();
        opset.add("update");
        opset.add("insert");
        opset.add("delete");
        if (!opset.contains(operation)){
            resultMap.put("code",-1);
            resultMap.put("msg","无此操作");
            return resultMap;
        }
        SqlSession sqlSession = SqlSessionFactoryUtil.openSession();
        CommonDao commonDao = sqlSession.getMapper(CommonDao.class);

        if (operation!=null&&operation.equals("update")){
            commonDao.updateTable(table,json.get(0),json.get(1));
        }else if (operation!=null&&operation.equals("insert")){
            commonDao.insertTable(table,json.get(0));
        }else if (operation!=null&&operation.equals("delete")){
            commonDao.deleteTable(table,json.get(0));
        }
        sqlSession.commit();
        sqlSession.close();
        resultMap.put("code",1);
        resultMap.put("msg","操作成功");
        return resultMap;
    }


    /**
     * 查询任一表所有记录
     * @param table 表名
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/data/get/{table}", produces = "application/json; charset=utf-8") //用来处理前台的logi
    public Map getTableData(@PathVariable("table") String table) throws IOException {

        HashMap<String,Object> resultMap=new HashMap<String, Object>();
        SqlSession sqlSession = SqlSessionFactoryUtil.openSession();
        CommonDao commonDao = sqlSession.getMapper(CommonDao.class);
        resultMap.put("resultdata",commonDao.getTableAll(table));
        sqlSession.close();
        resultMap.put("code",1);
        resultMap.put("msg","操作成功");
        System.out.print(resultMap);
        return resultMap;
    }

    /**
     * 根据查询条件和列 表中记录
     * @param table 表名
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/data/getby/{table}", produces = "application/json; charset=utf-8")
    public Map getTableDataByWhere(@PathVariable("table") String table,
                                   @RequestBody List<HashMap> json) throws IOException {
        System.out.println(json);
        SqlSession sqlSession = SqlSessionFactoryUtil.openSession();
        HashMap<String,Object> resultMap=new HashMap<String, Object>();
        try {
            CommonDao commonDao = sqlSession.getMapper(CommonDao.class);
            if (json.size()>1){
                List<HashMap> list=commonDao.getTableByWhere(table,(ArrayList) json.get(0).get("field"),json.get(1));
                resultMap.put("resultdata",list);
            }
            sqlSession.close();
            resultMap.put("code",1);
            resultMap.put("msg","操作成功");
        }catch (Exception e){
            resultMap.put("code",-1);
            resultMap.put("msg",e.getMessage());
            sqlSession.close();
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 更具任意sql语句查询
     * @param sql
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "view/getbysql",produces = "application/json;charset=utf-8")
    public Map getTableBysql(@RequestParam("sql") String sql){
        SqlSession sqlSession = SqlSessionFactoryUtil.openSession();
        HashMap<String,Object> resultMap=new HashMap<String, Object>();
        try {
            CommonDao commonDao = sqlSession.getMapper(CommonDao.class);
            List<HashMap> list=commonDao.executeSql(sql);
            resultMap.put("data",list);
            sqlSession.close();
            resultMap.put("code",1);
            resultMap.put("msg","操作成功");
        }catch (Exception e){
            resultMap.put("code",-1);
            resultMap.put("msg",e.getMessage());
            sqlSession.close();
            e.printStackTrace();
        }
        return resultMap;
    }
}
